package wb.is.besti.edu.cd.emergency_responsesys.pages;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import wb.is.besti.edu.cd.emergency_responsesys.R;
import wb.is.besti.edu.cd.emergency_responsesys.ui.AddMessageActivity;
import wb.is.besti.edu.cd.emergency_responsesys.utils.News;
import wb.is.besti.edu.cd.emergency_responsesys.utils.NewsService;
import wb.is.besti.edu.cd.emergency_responsesys.widget.NewsRecyclerAdapter;

public class FirstPageFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<News> newsList;
    private NewsRecyclerAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.first_news_fragment,container,false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(FirstPageFragment.this.getContext());
        recyclerView=(RecyclerView)view.findViewById(R.id.news_recycler);
        new Thread(){
            @Override
            public void run() {
                try {
                    newsList = NewsService.getJSONLastNews();
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
        }.start();
        while (newsList==null);
        adapter = new NewsRecyclerAdapter(newsList,getContext());
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);
        return view;
    }
    private void initPersonData() {
        newsList = new ArrayList<>();
        newsList.add(new News(getString(R.string.news_one_title),getString(R.string.news_one_desc),R.mipmap.news_one));
        newsList.add(new News(getString(R.string.news_two_title),getString(R.string.news_two_desc),R.mipmap.news_two));
        newsList.add(new News(getString(R.string.news_three_title),getString(R.string.news_three_desc),R.mipmap.news_three));
        newsList.add(new News(getString(R.string.news_four_title),getString(R.string.news_four_desc),R.mipmap.news_four));
    }
}
